package com.example.Config;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.logging.Slf4jLogFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.example.Mybatis.DynamicDataSource.DynamicDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@PropertySource("classpath:jdbc.properties")
@Configuration
@MapperScan("com.example.Mybatis.MapperDao")
public class MybatisConfig {

    @Autowired
    Environment environment;
    @Autowired
    Slf4jLogFilter Slf4jLogFilter;


    @Bean(name = "business", autowire = Autowire.BY_TYPE)
    public DataSource businessDataSource() throws PropertyVetoException,
            SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(environment.getProperty("dataSource.business.driverClassName"));
        dataSource.setUrl(environment.getProperty("dataSource.business.url"));
        dataSource.setUsername(environment.getProperty("dataSource.business.username"));
        dataSource.setPassword(environment.getProperty("dataSource.business.password"));
        dataSource.setInitialSize(Integer.parseInt(environment.getProperty("dataSource.business.InitialSize")));
        dataSource.setMaxActive(Integer.parseInt(environment.getProperty("dataSource.business.MaxActive")));
        dataSource.setMinIdle(Integer.parseInt(environment.getProperty("dataSource.business.MinIdle")));
        dataSource.setMaxWait(Integer.parseInt(environment.getProperty("dataSource.business.MaxWait")));
        dataSource.setFilters("log4j");
        List <Filter> filters = new ArrayList <>();
        filters.add(Slf4jLogFilter);
        dataSource.setProxyFilters(filters);
        return dataSource;
    }

    @Bean(name = "order", autowire = Autowire.BY_TYPE)
    public DataSource orderDataSource() throws PropertyVetoException,
            SQLException {

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(environment.getProperty("dataSource.order.driverClassName"));
        dataSource.setUrl(environment.getProperty("dataSource.order.url"));
        dataSource.setUsername(environment.getProperty("dataSource.order.username"));
        dataSource.setPassword(environment.getProperty("dataSource.order.password"));
        dataSource.setInitialSize(Integer.parseInt(environment.getProperty("dataSource.order.InitialSize")));
        dataSource.setMaxActive(Integer.parseInt(environment.getProperty("dataSource.order.MaxActive")));
        dataSource.setMinIdle(Integer.parseInt(environment.getProperty("dataSource.order.MinIdle")));
        dataSource.setMaxWait(Integer.parseInt(environment.getProperty("dataSource.order.MaxWait")));
        dataSource.setFilters("log4j");
        List <Filter> filters = new ArrayList <>();
        filters.add(Slf4jLogFilter);
        dataSource.setProxyFilters(filters);
        return dataSource;
    }

    @Bean(name = "equipment", autowire = Autowire.BY_TYPE)
    public DataSource equipmentDataSource() throws PropertyVetoException,
            SQLException {

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(environment.getProperty("dataSource.equipment.driverClassName"));
        dataSource.setUrl(environment.getProperty("dataSource.equipment.url"));
        dataSource.setUsername(environment.getProperty("dataSource.equipment.username"));
        dataSource.setPassword(environment.getProperty("dataSource.equipment.password"));
        dataSource.setInitialSize(Integer.parseInt(environment.getProperty("dataSource.equipment.InitialSize")));
        dataSource.setMaxActive(Integer.parseInt(environment.getProperty("dataSource.equipment.MaxActive")));
        dataSource.setMinIdle(Integer.parseInt(environment.getProperty("dataSource.equipment.MinIdle")));
        dataSource.setMaxWait(Integer.parseInt(environment.getProperty("dataSource.equipment.MaxWait")));
        dataSource.setFilters("log4j");
        List <Filter> filters = new ArrayList <>();
        filters.add(Slf4jLogFilter);
        dataSource.setProxyFilters(filters);
        return dataSource;
    }


    @Bean
    public DynamicDataSource getDynamicDataSource() throws PropertyVetoException, SQLException {
        DynamicDataSource DynamicDataSource = new DynamicDataSource();
        Map <Object, Object> targetDataSources = new HashMap <Object, Object>();
        targetDataSources.put("business", businessDataSource());
        targetDataSources.put("order", orderDataSource());
        targetDataSources.put("equipment", equipmentDataSource());
        DynamicDataSource.setTargetDataSources(targetDataSources);
        DynamicDataSource.setDefaultTargetDataSource(businessDataSource());
        return DynamicDataSource;

    }

    @Bean
    public JdbcTemplate getJdbcTemplate() throws PropertyVetoException,
            SQLException {
        JdbcTemplate JdbcTemplate = new JdbcTemplate(getDynamicDataSource());
        return JdbcTemplate;

    }

    /**
     * 配置mybatis动态数据源
     *
     * @return
     */
    @Bean(name = "SqlSessionFactoryBean")
    public SqlSessionFactoryBean getSqlSessionFactoryBean()
            throws PropertyVetoException, SQLException {
        SqlSessionFactoryBean SqlSessionFactoryBean = new SqlSessionFactoryBean();
        SqlSessionFactoryBean.setDataSource(getDynamicDataSource());
        return SqlSessionFactoryBean;

    }

    /**
     * 配置mybatis动态数据源
     *
     * @return
     */
    public MapperScannerConfigurer MapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("SqlSessionFactoryBean");
//        mapperScannerConfigurer.setBasePackage("com.example.Mybatis.MapperDao");
        return mapperScannerConfigurer;
    }

    /**
     * 配置事务 暂未测试
     *
     * @return
     * @throws PropertyVetoException
     * @throws SQLException
     */
    @Bean
    public DataSourceTransactionManager getDataSourceTransactionManager()
            throws PropertyVetoException, SQLException {
        DataSourceTransactionManager DataSourceTransactionManager = new DataSourceTransactionManager();
        DataSourceTransactionManager.setDataSource(getDynamicDataSource());
        return DataSourceTransactionManager;

    }
}
